/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Vue;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;

import App.Vue.Panneau_ParametragePartie.boutonListener;

public class Panneau_NouveauJoueur extends JPanel_Principal {
	
	private JButton validerBouton;
	private JButton retourBouton;
	public JTextField champNomJoueur;
	private JLabel labelNomPartie;
	private JPanel panelInterieur;
	private GridBagConstraints gbc;
	protected Panneau_CreationPartie partie;
	private Image parchemin;
	private boolean flagInit = true;

	public Panneau_NouveauJoueur(Panneau_CreationPartie partie) {
		super();

		this.partie = partie;
		this.panelInterieur = new JPanel();
		this.validerBouton = new JButton("Valider");
		this.retourBouton = new JButton("Retour");
		this.champNomJoueur = new JTextField();
		this.labelNomPartie = new JLabel("Nom du joueur");

		validerBouton.setFont(boutonFont);
		retourBouton.setFont(boutonFont);
		champNomJoueur.setFont(boutonFont);
		labelNomPartie.setFont(boutonFont);
		
		validerBouton.addActionListener(new boutonListener("Valider"));
		retourBouton.addActionListener(new boutonListener("Retour"));
		
		// Layout des JPanel
		this.gbc = new GridBagConstraints();
		this.setLayout(new GridBagLayout());
		this.panelInterieur.setLayout(new GridLayout(3, 2, 10, 10));

		this.panelInterieur.setBorder(new EmptyBorder(50, 50, 20, 50) );

		// Réglage de la transparence des fonds et des bordures
		this.setBackground(TRANSPARENT);
		this.setOpaque(false);
		this.panelInterieur.setOpaque(false);
		this.panelInterieur.setBackground(TRANSPARENT);
		this.champNomJoueur.setOpaque(false);
		this.champNomJoueur.setBorder(javax.swing.BorderFactory
				.createEmptyBorder());

		// Ajout des éléments graphiques au panel intérieur
		this.panelInterieur.add(labelNomPartie);
		this.panelInterieur.add(champNomJoueur);
		this.panelInterieur.add(validerBouton);
		this.panelInterieur.add(retourBouton);

		// Centrer le panel intérieur
		gbc.gridy = 1;
		gbc.gridx = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;

		this.add(panelInterieur, gbc);

		// DEBUG
		this.setBackground(Color.GRAY);// Couleur pour les tests!
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(flagInit){
			Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
			try {
				BufferedImage bufferedFond = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/parcheminH.png"));
				parchemin = bufferedFond.getScaledInstance(panelInterieur.getWidth(),panelInterieur.getHeight(), 0);

			} catch (IOException e) {
				e.printStackTrace();
			}
			flagInit = false;
		}
		g.drawImage(parchemin, 0, 0, this);
	}

	class boutonListener implements ActionListener{

		String bouton;
		public boutonListener(String bouton){
			super();
			this.bouton = bouton;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			//TODO : Notification au controleur
			switch (bouton) {
			case "Valider" ://Reinitialise la disposition
				System.out.println(champNomJoueur.getText());
				partie.validerJoueur(champNomJoueur.getText());
				break;
			case "Retour":
				partie.retourJoueur();
				break;
			default :
			}
		}
	}

}
